package com.example.demoes.message.listener;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.spring.annotation.ConsumeMode;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;

@Slf4j
@RocketMQMessageListener(topic = "TEST",
        consumerGroup = "test",
        consumeMode = ConsumeMode.ORDERLY)
@Component
public class TestMessageListener implements RocketMQListener<Message> {

    @Override
    public void onMessage(Message message) {
        try {
            byte[] body =  message.getBody();
            String s = new String(body, StandardCharsets.UTF_8);
            log.info("待消费的区划消息内容是：{}",s);
            /*String timestamp = msg.getProperties().get("timestamp");
            DateTime msgTime = new DateTime(Long.parseLong(timestamp));
            DateTime dateTime = DateUtil.parse(consumerDate,"yyyy-MM-dd HH:mm:ss");
            log.info("nacos中的dateTime{}", consumerDate);
            log.info("添加区划的消息时间{}", msgTime);
            if(msgTime.isBefore(dateTime)){
                log.info("消息日期太久了，我不消费了，我要返回！");
                return;
            }*/
            JSONObject jsonObject = JSONObject.parseObject(s);
            String messageType = jsonObject.getString("messageType");

            String data = jsonObject.getString("payload");
            JSONObject dataObj = JSONObject.parseObject(data);
            log.info("messageType:{},data:{}",messageType,dataObj);
        }catch (Exception e) {
            e.printStackTrace();
        }
    }
}
